﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Net;
using System.Diagnostics;
using Newtonsoft.Json;
using System.IO;
using System.Text.RegularExpressions;

namespace SkyDriveLib
{
    class SkyDriveLib
    {
        public SkyDriveLib() { }

        public void connect(string ClientID)
        {
            var uri = "https://login.live.com/oauth20_authorize.srf";

            var authorizeUri = new StringBuilder(uri);
            StringBuilder auth = new StringBuilder();
            authorizeUri.AppendFormat("?client_id={0}&", ClientID);
            authorizeUri.AppendFormat("scope={0}&", "wl.signin");
            authorizeUri.AppendFormat("response_type={0}&", "token");
            authorizeUri.AppendFormat("redirect_uri={0}", HttpUtility.UrlEncode("http://paulonicolau.eu/"));

            var startInfo = new ProcessStartInfo();
            startInfo.FileName = authorizeUri.ToString();
            Process.Start(startInfo);
        }
        

        public UserObj getUserDetail()
        {
            
            var access_token = "EwAwAq1DBAAUGCCXc8wU/zFu9QnLdZXy%2bYnElFkAAcUHR80Woa8ZGbgZNICMV%2bXJfuKCqWWRq0r7PYo5Z%2b8aKUZp/Rvx5sp94NvSj4bYNBWFpfpB/4DDR60/L22GQyaS4Nbqv211VccipVOdkj7p6W5sWHNSWmAHR91K2PwaH0NwYxPTrVK9BBlkBhkm0xNIHTdRXjp%2b/QXRRHAF6xurqC5fGXTzezFTFqhXDdtye2VzHlINtLLmAHZ0fP%2blXGo%2b9M%2b8Jlw7/D3MqCUEEVbvECImiK/UZmhTHQg/uXcd/uRIrsgZqrv1Ur7vXzMiSEBWO%2bhrkgWf6MDeVS9ppdyP93knOoLtb7RaQdmjSLUMUszKU5zFLsAQYZd4rDZzvbEDZgAACLBULE50XF7zAAECO1lMPSovpTp0MPx6g3K/fzHVHbOeZCJAyu3uAFvWXyuiDKkBATAaozRPn%2b9iOS9hPThZ%2bRPruQ9tmvaO1E9yrnd4FBA0jOeKPlspX4PLat2G2fF0YM/EmhIx9Ise48XjeeEZvOMOUht2nbf%2bDlk7Upg3qX3CXrb0vGIc43lAlZvhNGI5kbIjexBsFCN8cNw9C4nxF2lhIAw3BIzsKslvJXoHGNY1zPNnhYNDuqZtZUocjdeU6cCA0hKMpHl%2bha7PiMTHTHbnFigQRw9mHVEfcFv%2bUo0dSoU3qnGjbwZENfDnpOX7M%2bcgaTmkVHEzpPnZ6ZpGj9bhs1nGQ9u87AliAAA%3d&authentication_token=eyJhbGciOiJIUzI1NiIsImtpZCI6IjAiLCJ0eXAiOiJKV1QifQ.eyJ2ZXIiOjEsImlzcyI6InVybjp3aW5kb3dzOmxpdmVpZCIsImV4cCI6MTM2OTQ4ODkxMiwidWlkIjoiODgzZDYyYTU5OGU3NGNkMWVhZWViZjQyYTU0YzRmOGYiLCJhdWQiOiJwYXVsb25pY29sYXUuZXUiLCJ1cm46bWljcm9zb2Z0OmFwcHVyaSI6ImFwcGlkOi8vMDAwMDAwMDA0MDBGQUM0NiIsInVybjptaWNyb3NvZnQ6YXBwaWQiOiIwMDAwMDAwMDQwMEZBQzQ2In0.ToJDco6Wbr4REn6KSzEzgva0Wbr8VOGVU1TjFD4lEbw";
            var requestUri = new StringBuilder("https://apis.live.net/v5.0/me");
            requestUri.AppendFormat("?access_token={0}", access_token);
            var request = (HttpWebRequest)WebRequest.Create(requestUri.ToString());
            request.Method = "GET";
            
            var response = (HttpWebResponse)request.GetResponse();
            UserObj user;
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                var json = reader.ReadToEnd();

                user = JsonConvert.DeserializeObject<UserObj>(json);

            }
            return user;
             
        }

        public HttpStatusCode GetHeaders(string url)
        {
            HttpStatusCode result = default(HttpStatusCode);

            var request = HttpWebRequest.Create(url);
            request.Method = "HEAD";
            using (var response = request.GetResponse() as HttpWebResponse)
            {
                if (response != null)
                {
                    result = response.StatusCode;
                    response.Close();
                }
            }

            return result;
        }


    }
}
